package com.cssl.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.cssl.pojo.Emp;
import com.cssl.pojo.Users;
import com.cssl.service.EmpService;
import com.cssl.service.UsersService;
import io.micrometer.common.util.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RestController;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
public class PlusController {

    @Autowired
    private UsersService service;
    @Autowired
    private EmpService empService;

    @GetMapping("/saveOrUpdate")
    public Users saveUsers(Users user){
        service.saveOrUpdate(user);
        return user;
    }

    @GetMapping("/update")
    public Users update(Users user){
        service.update(new UpdateWrapper<Users>().set("password",user.getPassword())
                .eq("username",user.getUsername()));
        return user;
    }

    @GetMapping("/find/{index}/{size}")
    public List<Users> find(@PathVariable("index") long index,
                            @PathVariable("size") long size){
        return service.list(new Page<Users>(index,size),
                new QueryWrapper<Users>()
                        .between("id",1,3)
                        .or().like("username","李")
                        .orderByDesc("id"));
    }

    @GetMapping("/find2")
    public List<Users> find2(Users user){
        LambdaQueryWrapper<Users> query = Wrappers.lambdaQuery();
        query.eq(!StringUtils.isBlank(user.getUsername()), Users::getUsername, user.getUsername());
        //return service.list(query);

        Map<String,Object> map = new HashMap<>();
        map.put("id",1);
        map.put("username","李总");
        map.put("password","");
        //return service.listByMap(map);
        return service.list(
                new QueryWrapper<Users>().allEq(map,false));
    }

    @GetMapping("/remove/{id}")
    public boolean remove(@PathVariable("id") int id){
        //return service.removeById(id);
        return service.removeByMap(Map.of("username","李总"));
    }

    @GetMapping("/findEmp/{index}/{size}")
    public IPage<Emp> findEmp(@PathVariable("index") long index,
                              @PathVariable("size") long size){

        return empService.find(new Page<Emp>(index,size),
                new QueryWrapper<Emp>()
                        .between("empno",1,20)
                        .orderByDesc("empno"));
    }
}
